#ifndef __SOCKETUTILS__H__
#define __SOCKETUTILS__H__

#include "core.h"
#include <arpa/inet.h>
#include <netinet/in.h>
#include <sys/socket.h>

typedef struct sockaddr_in SocketAddress;

typedef struct TcpSocket {
    int socket;
    SocketAddress address;
} TcpSocket, TcpSocketServer;

void setSocketAddress(SocketAddress *address, sa_family_t family, int addr, int port);
bool setSocketConnectAddress(SocketAddress *address, sa_family_t family, const char *addr, int port);

// ------------------------------------------------------

bool socketSetOption(TcpSocket *tcp, int level, int option_name, const void *option_value, socklen_t option_len);
/*绑定套接字端口*/
bool socketBind(TcpSocket *tcp, const struct sockaddr *address, socklen_t address_len);
/*监听客户端请求*/
bool socketListen(TcpSocket *tcp, int backlog);
/*接收客户请求*/
bool socketAccept(TcpSocket *tcp, TcpSocket *client);
/*发送链接服务器请求*/
bool socketConnect(TcpSocket *tcp, const SocketAddress *address, socklen_t address_len);

// ------------------------------------------------------

bool createTcpSocket(TcpSocket *tcp);
bool createTcpSocketServer(TcpSocketServer *tcp, int port);
bool createTcpSocketWithHostPort(TcpSocket *tcp, const char *host, int port);

// ------------------------------------------------------
ssize_t socketRead(int socket, char *buffer, size_t len);
ssize_t socketWrite(int socket, char *buffer, size_t len);

// ------------------------------------------------------
ssize_t tcpSocketRead(TcpSocket *tcp, char *buffer, size_t len);
ssize_t tcpSocketWrite(TcpSocket *tcp, char *buffer, size_t len);

#endif  //!__SOCKETUTILS__H__